package cn.lg.soar.common.model;

import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.exceptions.BaseException;
import io.swagger.annotations.ApiModelProperty;

/**
 * 统一http响应结果
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class HttpResult<T> implements IHttpResult<T> {

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "错误码（A是客户端错误，B是服务端错误，C是第三方错误）")
    private String code;

    @ApiModelProperty(value = "信息内容")
    private I18n message;

    @ApiModelProperty(value = "信息参数")
    private Object[] args;

    /**
     * 响应请求失败的状态
     * @param code 错误码
     * @param message 错误信息
     * @param args 信息参数
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> fail(String code, I18n message, Object...args) {
        return new HttpResult<>(code, message, args);
    }

    public static <T> HttpResult<T> fail(String code, String message, Object...args) {
        return new HttpResult<>(code, I18n.build(message), args);
    }

    /**
     * 响应请求失败的状态
     * @param exception 失败异常
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> fail(BaseException exception) {
        HttpResult<T> result = fail(exception.getCode(), exception.getMsg(), exception.getMsgArgs());
        return result;
    }

    /**
     * 响应请求失败的状态（兜底）
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> fail() {
        return fail("B0000", I18n.build("服务器繁忙！请稍后重试！"));
    }

    /**
     * 响应请求成功的状态
     * @param message 成功信息
     * @param args 信息参数
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> success(I18n message, Object...args) {
        return new HttpResult<>(null, message, args);
    }

    public static <T> HttpResult<T> success(String message, Object...args) {
        return new HttpResult<>(null, I18n.build(message), args);
    }

    /**
     * 响应请求成功的状态
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> success() {
        return HttpResult.success("操作成功");
    }

    /**
     * 响应请求成功返回数据
     * @param data 响应的数据
     * @param <T>
     * @return
     */
    public static <T> HttpResult<T> data(T data) {
        return new HttpResult<>(data);
    }

    public HttpResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public T getData() {
        return data;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        if (message == null) {
            return null;
        }
        return message.msg(args);
    }

    public Object[] getArgs() {
        return args;
    }

    public String getMsgCode() {
        if (message == null) {
            return null;
        }
        return message.code();
    }

    @ApiModelProperty(value = "是否成功")
    public Boolean getSuccess() {
        return code == null;
    }

    private HttpResult(T data) {
        this.data = data;
    }

    private HttpResult(String code, I18n message, Object...args) {
        this.code = code;
        this.message = message;
        this.args = args;
    }

}
